<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div style="margin-bottom: 10px;">
                        <div class="layui-input-item" style="float:left;padding-right:10px;">
                            <form class="layui-form">
                                <div class="layui-inline">
                                    <input id="search-owner-name" type="text"
                                           placeholder="请输入姓名" class="layui-input layui-input-search">
                                </div>
                                <div class="layui-inline">
                                    <input id="search-name" type="text"
                                           placeholder="请输入任务名称" class="layui-input layui-input-search">
                                </div>
                                <div class="layui-inline">
                                    <a id="btn-search" class="layui-btn">查找</a>
                                </div>
                            </form>
                        </div>
                        <div style="clear:both;"></div>
                    </div>
                    <table class="layui-hide" id="task-table" lay-filter="task-table"></table>
                    <script type="text/html" id="toolbar-operation">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">详情</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="task-form">
    <div class="layui-card">
        <div class="layui-card-header">流程任务详情</div>
        <div class="layui-card-body">
            <form class="layui-form" action="#" lay-filter="task-form">
                <input type="hidden" name="id">
                <div class="layui-form-item">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="ownerName" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">*任务名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" class="layui-input" lay-verify="required">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">*创建时间</label>
                    <div class="layui-input-block">
                        <input type="text" name="createTime" class="layui-input" lay-verify="required">
                    </div>
                </div>
            </form>
        </div>
    </div>
</script>
<script src="/layui/layui.js"></script>
<script src="/js/common.js"></script>
<script>
    layui.config({
        base: '/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'form', 'laydate'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var admin = layui.admin;
        var laydate = layui.laydate;
        var popupIndex;
        var procInstId = getParam('id');

        table.render({
            elem: '#task-table',
            url: 'tasks',
            title: '',
            skin: 'line',
            cols: [[
                {field: 'ownerName', title: '姓名'},
                {field: 'name', title: '任务名称'},
                {field: 'createTime', title: '开始时间'},
                {field: 'completeTime', title: '完成时间'},
                {fixed: 'right', title: '操作', toolbar: '#toolbar-operation'}
            ]],
            where: {
                procInstId,
                ownerName: $('#search-owner-name').val(),
                name: $('#search-name').val(),
            },
            page: true
        });

        function showTaskForm() {
            popupIndex = admin.popupRight({
                area: '360px',
                success: function () {
                    var formHtml = $('#task-form').html();
                    $('#' + this.id).html(formHtml);
                    form.render();
                    laydate.render({
                        elem: '#create-time'
                    });
                }
            });
            return popupIndex;
        }

        function closePopupForm() {
            if (popupIndex) {
                layer.close(popupIndex);
            }
        }

        function search() {
            table.reload('task-table', {
                where: {
                    procInstId,
                    ownerName: $('#search-owner-name').val(),
                    name: $('#search-name').val(),
                },
                page: {
                    curr: 1
                }
            });
        }

        $(document).on('keypress', function (e) {
            if (e.which == 13) {
                search();
            }
        });

        $("#btn-search").on('click', function () {
            search();
        });

        $("#btn-add").on('click', function () {
            popupIndex = showTaskForm();
        });

        $("#btn-refresh").on('click', function () {
            table.reload('task-table');
            layer.msg('表格已刷新');
        });

        table.on('tool(task-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除该记录吗？', function (index) {
                    $.ajax({
                        url: 'tasks/' + data.id,
                        type: 'DELETE',
                        success: function (result) {
                            if (result.code == 200) {
                                layer.msg('删除成功');
                                table.reload('task-table');
                            } else {
                                layer.msg('删除失败');
                            }
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                showTaskForm();
                form.val('task-form', data);
            }
        });

    });
</script>
</body>
</html>